import jwt from 'jsonwebtoken'
import config from '../config'

export const sign = async user => {
  const expiresIn = 60 * 60 * 24 * 15 // days
  let userToken = { id: user.id, roles: user.roles }
  const token = jwt.sign({ user: userToken }, config.jwt.secret, {
    expiresIn
  })
  return {
    id: user.id,
    access_token: token,
    expires_in: expiresIn,
    user_id: user.id,
    nick_name: user.nickName,
    avatar: user.avatarUrl
  }
}
